/*
	Copyright (C) 2008 by Jean-François Geyelin
	jfgeyelin@gmail.com

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the
	Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/



#include "all.h"
#include "SurfaceHandler.h"
#include "globals.h"
#include "DisplayString.h"
#include "safeFileIO.h"
#include "ThreadParameters.h"
#include "downloadFileFromServer.h"
#include "drawButtonInMenu.h"
#include "handleEvents.h"
#import "url.h"


bool getSeedFromServer(int *seed) {

    SurfaceHandler::colorScreen(255,255,255);

    *seed = 0;


    SDL_Thread *thread;

    ThreadParameters *tp = new ThreadParameters(URL_OF_SERVER"/GetSeed","save/seed_temp.txt");

    thread = SDL_CreateThread(getFileFromServerThread, tp);



    std::string s = "Waiting for server";
    std::string s3 = "";

    int mouseX;
    int mouseY;
    bool mouseIsPressed;
    bool seedCouldNotBeObtained = false;
    int time = 0;

    while (!quit && *seed == 0) {





        if (tp->getFileIsDownloaded()) {
            if (tp->getThereHasBeenAnError()) {
                seedCouldNotBeObtained = true;
            }
            else
            {
                FILE *f = fopen_safe("save/seed_temp.txt","rb");
                fscanf(f,"%i",seed);
                fclose(f);
                remove_safe("save/seed_temp.txt");
            }
        }





        SDL_Rect r;
        r.x = 0;
        r.y = SCREEN_HEIGHT/2 - 30;
        r.w = SCREEN_WIDTH;
        r.h = 16+20;
        SDL_FillRect(screenSurface, &r,SDL_MapRGB( screenSurface->format, 255,255,255 ));




        time++;
        if (time>=400 && !seedCouldNotBeObtained) {
            std::string s2;
            if (time>800)
            s2 = "It is taking much longer than usual.";
            else
            s2 = "It is taking longer than usual.";
            DisplayString::drawString3(s2.c_str(), (SCREEN_WIDTH-(FONT_SPACING*s2.length()))/2,SCREEN_HEIGHT/2 - 10);
        }

        mouseIsPressed = SDL_GetMouseState(&mouseX,&mouseY)&SDL_BUTTON(1);

        std::string s4 = s+s3;
        if (time % 10 == 0) {
            s3+=".";
            if (s3.length() == 4)
            s3 = "";
        }



        if (!seedCouldNotBeObtained)
        DisplayString::drawString3(s4.c_str(), (SCREEN_WIDTH-(FONT_SPACING*s.length()))/2,SCREEN_HEIGHT/2 - 30);
        else
        DisplayString::drawString3("Failed to contact server.", (SCREEN_WIDTH-(FONT_SPACING*25))/2,SCREEN_HEIGHT/2 - 30);

        quit |= drawButtonInMenu(mouseX, mouseY, (SCREEN_WIDTH-50)/2, SCREEN_HEIGHT/2 + 30, 50 , 20 , "cancel", screenSurface,1,1) && mouseIsPressed;

        SDL_Flip( screenSurface );
		SDL_Delay(15);
		handleEvents();
    }

    while (quit) {
        quit = false;
        handleEvents();
        SDL_Delay(20);
    }

    if (*seed==0) {
        if (!tp->getFileIsDownloaded())
        SDL_KillThread(thread);
        delete tp;
        return false;
    }
    else {
        delete tp;
        return true;
    }
}


